1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
| def get(self,request): filename = '导入模板' + '.xls' # 设置HTTPResponse的类型 reposnse = HttpResponse(content_type='application/vnd.ms-excel') # 创建一个文件对象 reposnse['Content-Disposition'] = 'attachment;filename='+filename # 创建一个sheet对象 wb = xlwt.Workbook(encoding='utf-8') sheet = wb.add_sheet('order-sheet') # 设置文件头的样式 style_heading = xlwt.easyxf(""" font: name Arial, colour_index white, bold on, height 0xA0; align: wrap off, vert center, horiz center; pattern: pattern solid, fore-colour 0x19; borders: left THIN, right THIN, top THIN, bottom THIN; """)
# 写入文件标题 sheet.write(0, 0, '姓名', style_heading) sheet.write(0, 1, '手机号', style_heading) sheet.write(0, 2, '身份证号', style_heading) sheet.write(0, 3, '区域号', style_heading) # 写出到IO output = BytesIO() wb.save(output) output.seek(0) reposnse.write(output.getvalue()) logger.info('用户:%s 导出了导入模板' % (request.user.username)) return reposnse
|